package ru.tusur.asu.pnn.perceptron;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;

import org.apache.commons.lang.Validate;

public class Layer implements Iterable<ActivationFunction> {
	
	public static class Builder {
		private final LinkedList<ActivationFunction> functions = new LinkedList<ActivationFunction>();
		
		public Builder addNeurons(ActivationFunction func, int count) {
			Validate.notNull(func, "func should not be null.");
			
			functions.addAll(Collections.nCopies(count, func));
			return this;
		}
		
		public Builder addNeurons(ActivationFunction... functions) {	
			Validate.noNullElements(functions, "null functions are not allowed");
			
			this.functions.addAll(Arrays.asList(functions));
			return this;
		}
		
		public Layer create() {
			Layer res = new Layer();
			res.functions = new ActivationFunction[functions.size()];  
			functions.toArray(res.functions);			
			return res;
		}
	}
	
	private ActivationFunction[] functions;
			
	public Layer(ActivationFunction... functions) {
		Validate.isTrue(functions.length >= 0, "Number of neuron functions shouldn't be negative");
		this.functions = functions;
	}
	
	public Layer(ActivationFunction function, int count) {
		Validate.notNull(function, "function should not be null");
		Validate.isTrue(count >= 0, "count should be positive");
		
		functions = new ActivationFunction[count];
		Arrays.fill(functions, function);
	}
	
	public ActivationFunction get(int index) {
		return functions[index];
	}
	
	public int size() {
		return functions.length;
	}
	
	@Override
	public Iterator<ActivationFunction> iterator() {
		return Collections.unmodifiableList(Arrays.asList(functions)).iterator();
	}
	
	private Layer() {		
	}	 
}
